package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wmUser.dtos.WmUserDto;
import com.heima.model.wmUser.pojos.WmUser;
import com.heima.model.wmUser.vos.WmUserVO;
import com.heima.utils.common.AppJwtUtil;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {
    /**
     * 根据用户名查询自媒体用户
     *
     * @return
     */
    @Override
    public ResponseResult findWmUserByName(String name) {
        //判断用户名是否合法
        if (StringUtils.isBlank(name)) {
            CustomException.cust(AppHttpCodeEnum.PARAM_INVALID, "111111111");
        }
        //判断数据库中是否有该用户
        LambdaQueryWrapper<WmUser> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(WmUser::getName, name);
        WmUser wmUserByName = getOne(wrapper);
        if (wmUserByName != null) {
            CustomException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "22222222222");
        }
        return ResponseResult.okResult(wmUserByName);
    }

    /**
     * 保存自媒体用户
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult addWmUser(WmUser wmUser) {
        save(wmUser);
        return ResponseResult.okResult(wmUser);
    }

    /**
     * 自媒体用户登录
     */
    @Override
    public ResponseResult login(WmUserDto wmUserDto) {
        //在实体类中加入了判断非空的注解，因此此时不需要额外判断
        //根据用户名，判断数据库中是否有该用户
        LambdaQueryWrapper<WmUser> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(WmUser::getName, wmUserDto.getName());
        WmUser wmUser = getOne(wrapper);
        if (wmUser == null) {
            CustomException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "当前自媒体用户不存在");
        }
        //判断当前用户的状态
        if (wmUser.getStatus() != 9) {
            CustomException.cust(AppHttpCodeEnum.LOGIN_STATUS_ERROR);
        }
        //判断密码是否正确
        String salt = wmUser.getSalt();
        String pwd = DigestUtils.md5DigestAsHex((wmUserDto.getPassword() + salt).getBytes());
        if (!pwd.equals(wmUser.getPassword())) {
            CustomException.cust(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        //修改登录时间
        wmUser.setLoginTime(new Date());
        //设置token
        HashMap<String, Object> map = new HashMap<>();
        String token = AppJwtUtil.getToken(Long.valueOf(wmUser.getId()));
        WmUserVO vo = new WmUserVO();
        BeanUtils.copyProperties(wmUser, vo);
        map.put("token", token);
        map.put("user", vo);

        return ResponseResult.okResult(map);
    }
}
